import Pages from '../../../pages/pages.vue'
import MiniPrizeConfig from '../mini-prize-config/mini-prize-config.vue'
import { showMsg, prizeList, setMenuType, setPrizeFlag } from '../../../../vuex/action'

export default {
  name: 'prize-list',
  data() {
    return {
      prizeConfigTitle: '新增奖品',
      showMiniPrizeModal: false,
      prizeEx: {
        id: '',
        name: '',
        pic: '',
        activityId: +this.currentId,
        totalNum: null,
        rate: null,
        type: 0
      },
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      currentId: '',
      currentName: '',
      pList: []
    }
  },
  components: {
    'vue-mini-prize': MiniPrizeConfig,
    'vue-pages': Pages
  },
  vuex: {
    getters: {
      fresh: state => state.miniPrizeListNeedFresh,
      activityId: state => state.activityStoreId,
      activityName: state => state.activityStoreName
    },
    actions: {
      xShowMsg: showMsg,
      xPrizeList: prizeList,
      xSetMenuType: setMenuType,
      xSetPrizeFlag: setPrizeFlag
    }
  },
  ready() {
    if (this.activityId) {
      window.sessionStorage.setItem('currentId',this.activityId)
      window.sessionStorage.setItem('currentName',this.activityName)
    }
    this.currentId = window.sessionStorage.getItem('currentId')
    this.currentName = window.sessionStorage.getItem('currentName')

    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit, currentId=this.currentId}) {
      this.xPrizeList({
        start: start,
        limit: limit,
        activityId: currentId
      })
        .then((data) => {
          this.pList = data.list
          this.pages.totalPages = Math.ceil(data.totalcount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    addPrize() {
      this.prizeConfigTitle = '新增奖品'
      this.showMiniPrizeModal = true
      this.prizeEx = {
        name: '',
        pic: '',
        activityId: this.currentId,
        totalNum: null,
        rate: null,
        type: 0
      }
    },
    updatePrize(prize) {
      this.prizeConfigTitle = '修改奖品'
      this.showMiniPrizeModal = true
      this.prizeEx = {
        id: prize.id,
        name: prize.name,
        pic: '',
        picUrl: prize.pic,
        activityId: this.currentId,
        totalNum: +prize.totalNum,
        rate: +prize.rate,
        type: +prize.type,
        status: +prize.status,
        seedNum: prize.seedNum
      }
    },
    checkCodeList(id, name) {
      let data = {
        prizeId: +id,
        prizeName: name
      }
      this.xSetPrizeFlag(data)
      this.$route.router.go('/code/' + id)
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
